import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yt_devops/log_record/student.dart';

import 'db_manager.dart';

/**
 * 
 * @Description TODO
 * @Author liuer
 * @CreateTime 2023年11月21日 19:45
 */
class AddStudentPage extends StatefulWidget {
  const AddStudentPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AddStudentPageState();
  }
}

class _AddStudentPageState extends State<AddStudentPage> {
  int _sexValue = 0;
  late TextEditingController _nameController;
  late TextEditingController _ageController;

  @override
  void initState() {
    _nameController = TextEditingController();
    _ageController = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    _nameController.dispose();
    _ageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("增加学生"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            TextField(
              controller: _nameController,
              decoration: const InputDecoration(labelText: "姓名："),
            ),
            TextField(
              controller: _ageController,
              decoration: const InputDecoration(labelText: "年龄："),
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
            ),
            Row(
              children: [
                Expanded(
                  child: RadioListTile<int>(
                    title: const Text("女"),
                    value: 0,
                    groupValue: _sexValue,
                    onChanged: (value) {
                      setState(() {
                        _sexValue = value!;
                      });
                    },
                  ),
                ),
                Expanded(
                  child: RadioListTile<int>(
                    title: const Text("男"),
                    value: 1,
                    groupValue: _sexValue,
                    onChanged: (value) {
                      setState(() {
                        _sexValue = value!;
                      });
                    },
                  ),
                ),
              ],
            ),
            Builder(builder: (BuildContext context) {
              return Column(
                children: [
                  ElevatedButton(
                    onPressed: () async {
                      var student = Student(
                        name: _nameController.text.toString(),
                        age: int.parse(_ageController.text.toString()),
                        sex: _sexValue,
                      );
                      // int result = await DBManager().saveData(student);
                      // if (result > 0) {
                      //   // showSnackBar(context, '保存数据成功，result:$result');
                      //   print('保存数据成功，result:$result');
                      // } else {
                      //   print('保存数据失败，result:$result');
                      //   // showSnackBar(context, '保存数据失败，result:$result');
                      // }
                    },
                    child: const Text("保存数据"),
                  ),
                  ElevatedButton(
                    onPressed: () async {
                      var student = Student(
                        name: _nameController.text.toString(),
                        age: int.parse(_ageController.text.toString()),
                        sex: _sexValue,
                      );
                      int result = await DBManager().saveDataBySQL(student);
                      if (result > 0) {
                        print('保存数据成功，result:$result');
                        // showSnackBar(context, '保存数据成功，result:$result');
                      } else {
                        print('保存数据失败，result:$result');
                        // showSnackBar(context, '保存数据失败，result:$result');
                      }
                    },
                    child: const Text("使用SQL保存数据"),
                  ),
                ],
              );
            }),
          ],
        ),
      ),
    );
  }
}
